/*
 * $Id: ShemHourRecordReader.java,v 1.1 2009/06/18 19:57:15 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.shemjob.reader;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
/**
 * SHeM Hour Record Reader.
 *  Reads records from SHeM hour data files, translating them to ASCII EDM
 *   format.
 *
 * @author Daniel S. Horwitz, Compuware Corp.
 *
 * @version AutoSC P5P6
 */
public class ShemHourRecordReader extends ShemRecordReader {
    /**
     * Construct a SHeM record reader for reading records in hour.dat
     *  format.  It must be openned to use it.
     */
    public ShemHourRecordReader()
    {
        super();
    }

    /**
     * Get element map.  This returns the map of the elements in a source
     *  binary record.  The map specifies the size of the each of the
     *  elements in a source binary record, their location, and the number
     *  of times each element occurs.
     * This class's map represents the following C-oriented record layout:<br>
     *      #define MAX_FIELDS 17<br>
     *      packed struct newRec<br>
     *      {<br>
     *          long rawTime;<br>
     *          float rawAvgVal[MAX_FIELDS];<br>
     *          float rawMinVal[MAX_FIELDS];<br>
     *          float rawMaxVal[MAX_FIELDS];<br>
     *          char rawStatus[MAX_FIELDS];<br>
     *          short rawDin;<br>
     *          char rawDout;<br>
     *      };<br>
     *
     * @return The map of the elements in the source binary record.
     */
    protected int[] getElementMap()
    {
        return new int[] { 1, 4,
                           ShemRecordReader.MAX_FIELDS, 4,
                           ShemRecordReader.MAX_FIELDS, 4,
                           ShemRecordReader.MAX_FIELDS, 4,
                           ShemRecordReader.MAX_FIELDS, 1,
                           1, 2,
                           1, 1 };
    }

    
    /**
     * Read the body of a single record into the individual fields.  This
     *  reads all the fields of the source binary record.  The layout of
     *  the source record matches that of the element map defined above.
     *
     * @param sourceStream Stream for reading the binary data.  The values
     *  read from this stream have already been byte-swapped as appropriate.
     *
     * @exception EOFException If the end of the source stream is reached
     *  before the entire body of the record has been read.  This means
     *  that partial ending records are ignored.
     *
     * @exception IOException If an I/O error, other than end of stream, occurs.
     */
    protected void readOneRecordBody(DataInputStream sourceStream)
        throws EOFException, IOException
    {
        rawTime = sourceStream.readInt();
        for ( int idx = 0; idx < MAX_FIELDS; idx += 1 )
        {
            rawAvgVal[idx] = sourceStream.readFloat();
        }
        for ( int idx = 0; idx < MAX_FIELDS; idx += 1 )
        {
            rawMinVal[idx] = sourceStream.readFloat();
        }
        for ( int idx = 0; idx < MAX_FIELDS; idx += 1 )
        {
            rawMaxVal[idx] = sourceStream.readFloat();
        }
        for ( int idx = 0; idx < MAX_FIELDS; idx += 1 )
        {
            rawStatus[idx] = sourceStream.readByte();
        }
        rawDin = sourceStream.readShort();
        rawDout = sourceStream.readByte();
    }

    /**
     * Returns the spaces that appear immediately after the ASCII timestamp
     *  in the ASCII EDM format.  In this case, the string is one space.
     *
     * @return
     *   The field following the ASCII timestamp in the ASCII EDM format,
     *    which consists of one space.
     */
    protected String spacesAfterBrokenOutTime()
    {
        return " ";
    }
}
